package com.shigu.constant.service;

import com.opentae.data.mall.beans.ManagerPageConstant;
import com.opentae.data.mall.examples.ManagerPageConstantExample;
import com.opentae.data.mall.interfaces.ManagerPageConstantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.赵
 * created on 2018/11/14
 */
@Service
public class SystemConstantService {
    @Autowired
    public ManagerPageConstantMapper managerPageConstantMapper;

    private Map<String,Map<String,Map<String,String>>> OEM_PAGE_CONSTANT=new HashMap<>();//页面的常量

    private static final String webhost="127.0.0.1";
    /**
     * 更新页面常量
     */
    public void loadConstantValue(){
        Map<String,Map<String,String>> PAGE_CONSTANT=OEM_PAGE_CONSTANT.computeIfAbsent(webhost, o -> new HashMap<>());
        ManagerPageConstantExample example=new ManagerPageConstantExample();
        example.setOrderByClause("constant_id desc");
        List<ManagerPageConstant> constants=managerPageConstantMapper.selectByExample(example);
        for(ManagerPageConstant c:constants){
            Map<String,String> fieldMap=PAGE_CONSTANT.computeIfAbsent(c.getPageUri(),o -> new HashMap<>());
            fieldMap.put(c.getFieldName(),c.getFieldValue());
        }
    }

    /**
     * 查询常量
     * @param uri
     * @return
     */
    public Map<String,String> selPageConstant(String uri){
        Map<String,Map<String,String>> PAGE_CONSTANT=OEM_PAGE_CONSTANT.get(webhost);
        if (PAGE_CONSTANT == null) {
            loadConstantValue();
        }
        PAGE_CONSTANT=OEM_PAGE_CONSTANT.get(webhost);
        return PAGE_CONSTANT.get(uri);
    }
}
